package com.aaa.sbms.service;

import com.aaa.sbms.dao.QuitDao;
import com.aaa.sbms.util.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuitServiceImpl implements QuitService {

    @Autowired(required = false)
    private QuitDao quitDao;

    @Override
    public Map quitPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = quitDao.quitPage(map);
        int total = quitDao.quitPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map quitRecordPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = quitDao.quitRecordPage(map);
        int total = quitDao.quitRecordPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map quitOne(int id) {
        Map resultMap = new HashMap();
        Map list = quitDao.quitOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public int addQuit() {
        Map user = (Map) UserSession.getUser();
        int userid = Integer.valueOf(user.get("userid").toString());
        List<Map> isQuit = quitDao.isQuit(userid);
        if (isQuit.size()==0){//判断是否申请过了
            //如果没有就申请
            user = quitDao.user(userid);//根据userid重新查询出数据来？？？
//            String telephone = user.get("phone").toString();
//            user.put("telephone",telephone);
            int pid = Integer.valueOf(user.get("positionid").toString());
            user.put("pid",pid);
            return quitDao.addQuit(user);
        }else {
            return 0;//有就直接返回0
        }
    }

    @Override
    @Transactional
    public int pass(Map map) {
        int result = Integer.valueOf(map.get("result").toString());//审核结果
        int id = Integer.valueOf(map.get("id").toString());
        quitDao.updateQuitSetState(id);//修改离职申请表的审批状态
        quitDao.addQuitRecord(map);
        if (result==1){
            int userid = Integer.valueOf(map.get("userid").toString());
            quitDao.updateUsers(userid);//更新users的state
        }
        return 1;
    }
}
